package com.example;

import com.example.controller.HelloController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

//@ComponentScan({"com.example"})//配置扫描路径是com.example及其子包
@SpringBootApplication
public class Unit04SpringIocdiApplication {

    public static void main(String[] args) {
        //获取Spring上下文对象
        ApplicationContext context = SpringApplication.run(Unit04SpringIocdiApplication.class, args);
/*        //从Spring上下文中获取对象
        HelloCntroller helloCntroller = context.getBean(HelloCntroller.class);
        //使用对象
        helloCntroller.sayHi();

        //使用名称获取bean对象
        HelloCntroller helloCntroller1 = (HelloCntroller) context.getBean("helloCntroller");
        helloCntroller1.sayHi();*/

//        HelloService bean = (HelloService)context.getBean("helloService");
//        bean.sayHi();
//
//        HelloComponent bean1 = (HelloComponent)context.getBean("helloComponent");
//        bean1.sayHi();

//        UserInfo bean = context.getBean(UserInfo.class);//根据类型获取bean对象
//        System.out.println(bean);
//        UserInfo bean = (UserInfo) context.getBean("u2");//根据名称获取对象
//        System.out.println(bean);
//        UserInfo bean1 = (UserInfo) context.getBean("u1");//根据名称获取对象
//        System.out.println(bean1);
        //从Spring上下文中获取对象
//        HelloCntroller helloCntroller = context.getBean(HelloCntroller.class);
//        //使用对象
//        helloCntroller.sayHi();
        HelloController bean = context.getBean(HelloController.class);
        bean.sayHi();


    }

}
